angular.module('Controller')

.controller('QgyFinanceAuditCtrl', ($scope,NgTableParams,menuManage,urls,userData,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            status:undefined
            searchStr:undefined
            createStart:undefined
            createEnd:undefined
            apartmentId:$localStorage['accountStore']
            type:undefined
        }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.offLeaseStatusList=commonConstant.offLeaseStatusList(true)
    $scope.financeStatusList=commonConstant.financeStatusList(true)
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        reqData={
            page: params.page() - 1,
            size: params.count(),
            status:$scope.search.status
            searchStr:$scope.search.searchStr
            apartmentId:$scope.search.apartmentId
            type:$scope.search.type
        }
        console.log('$scope.search.createStart',$scope.search.createStart)
        reqData['createStart']=moment($scope.search.createStart).format('YYYY-MM-DD') if $scope.search.createStart
        reqData['createEnd']=moment($scope.search.createEnd).format('YYYY-MM-DD') if $scope.search.createEnd
        $scope.currentSearchData=$scope.search
        $http.get('/offLease',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={type:1,enabled:1}
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            showModal('edit',theData)
        confirmRefund:(item)->
            return if item.status!=8 #待退钱
            $http.put("/offLease/#{item.id}",{status:16})
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('确认退钱成功')
                    $scope.queryData()
                else
                    wfAlert.toastError(resp.message)
            )
        viewData:(item)->
            return
            theData=angular.copy(item)
            showModal('view',theData)
        resetSearch: ->
            initSearchData()
        auditData:(item)->
            theData=angular.copy(item)
            showAuditModal('audit',theData)
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        assignAppoint:(item)->
            showAssignModal('assign',angular.copy(item))
        cancelAppoint:(item)->
            showCancelModal(angular.copy(item))
        feedbackResult:(item)->
            showFeedbackModal('assign',angular.copy(item))
        exportData:->
            $scope.exporting = true;
            data = $scope.currentSearchData;
            reqData = {
                page:0
                size:99999
                status:data.status
                searchStr:data.searchStr
                apartmentId:data.apartmentId
                type:data.type
            }
            reqData['createStart']=moment(data.createStart).format('YYYY-MM-DD') if data.createStart
            reqData['createEnd']=moment(data.createEnd).format('YYYY-MM-DD') if data.createEnd
            $http.get('/offLease/excel',params:reqData)
            .success((resp)->
                $scope.exporting = false
                if resp.code == 0
                    filename = resp.message
                    url=urls.backend+'/offLease/download?file=' + filename
                    url=url+'&token='+userData.getToken()
                    console.log('url',url)
                    window.open(url, '_blank')
            )
            .error((err)->
                $scope.exporting = false
            )
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyAppointManageEditCtrl',
            templateUrl: 'templates/business-manage/appointment-manage/appointment-manage-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
    showAuditModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyFinanceAuditModalCtrl',
            templateUrl: 'templates/finance-service/finance-audit/finance-audit-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)